package com.qianfeng.openapi.web.master.service.impl;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.qianfeng.openapi.web.master.feign.CacheFeign;
import com.qianfeng.openapi.web.master.mapper.CustomerMapper;
import com.qianfeng.openapi.web.master.pojo.Customer;
import com.qianfeng.openapi.web.master.service.CustomerService;
import com.qianfeng.openplatform.common.SystemParams;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

/**
 * @Author LJF
 * @Date 2020/6/29 19:19
 * @Version 1.0
 */
@Service
@Transactional
public class CustomerServiceImpl implements CustomerService {

    @Autowired
    private CustomerMapper customerMapper;

    @Autowired
    private CacheFeign cacheFeign;

    @Override
    public int insertCustomer(Customer customer) {

        int i = customerMapper.insertCustomer(customer);
        try {
            cacheFeign.hMset(SystemParams.CUSTOMER_REDIS_PRE+customer.getId(),customer);
        } catch (Exception exception) {
            exception.printStackTrace();
        }
        return i;
    }

    @Override
    public int updateCustomer(Customer customer) {

        int i = customerMapper.updateCustomer(customer);
        try {
            cacheFeign.hMset(SystemParams.CUSTOMER_REDIS_PRE+customer.getId(),customer);
        } catch (Exception exception) {
            exception.printStackTrace();
        }
        return i;
    }

    @Override
    public int deleteCustomer(Long id) {
        return customerMapper.deleteCustomer(id);
    }

    @Override
    public Customer getCustomerById(Long id) {
        return customerMapper.getCustomerById(id);
    }

    @Override
    public List<Customer> getAllCustomers() {
        return customerMapper.getAllCustomers();
    }

    @Override
    public PageInfo<Customer> getCustomerByPage(Integer page,Integer size){
        PageHelper.startPage(page,size);
        List<Customer> customerList = customerMapper.getAllCustomers();
        return new PageInfo<>(customerList);
    }

    @Override
    public void deleteCustomers(Long[] ids) {
        if ( ids==null && ids.length==0){
            return;
        }
        for (Long id : ids) {
            Customer customerById = customerMapper.getCustomerById(id);
            int i = customerMapper.deleteCustomer(id);
            if (i==1 && customerById!=null){
                try {
                    cacheFeign.deleteKey(SystemParams.CUSTOMER_REDIS_PRE+customerById.getId());
                } catch (Exception exception) {
                    exception.printStackTrace();
                }
            }
        }
        customerMapper.deleteCustomers(ids);
    }

    @Override
    public PageInfo<Customer> selectCustomerByCondition(Integer page, Integer limit, String username, Integer state) {
        PageHelper.startPage(page,limit);
        List<Customer> customerList = customerMapper.selectCustomerByCondition(username,state);
        return new PageInfo<>(customerList);
    }


}
